import sys

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox

from tabUi import Ui_MainWindow


class Loginwindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(Loginwindow, self).__init__()
        self.setupUi(self)

    def setupUi(self, Ui_MainWindow):
        super().setupUi(Ui_MainWindow)
        self.pushButton.clicked.connect(self.addTab)
        self.pushButton_2.clicked.connect(self.removetab)
        self.pushButton_3.clicked.connect(self.deleteTab)

    def addTab(self):
        #创建选项卡对象
        self.adtab = QtWidgets.QWidget()
        #设置选项卡上的名称
        name = 'tab' + str(self.tabWidget.count() + 1)
        self.adtab.setObjectName(name)
        self.tabWidget.addTab(self.adtab, name)

    def removetab(self):
        #移除选中的选项卡
        self.tabWidget.removeTab(self.tabWidget.currentIndex())

    def deleteTab(self):
        self.getTab()

    def getTab(self):
        reply = QMessageBox.question(None, '确认删除',
                                     '你选中了:' + str(
                                         self.tabWidget.tabText(self.tabWidget.currentIndex())) + ',index为:' + str(
                                         self.tabWidget.currentIndex()) + '这个选项卡将被删除!'
                                     , QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Cancel)
        if reply == QMessageBox.Ok:
            self.tabWidget.removeTab(self.tabWidget.currentIndex())
        elif reply == QMessageBox.Cancel:
            QMessageBox.information(self, '已取消', '删除操作已取消', QMessageBox.Ok)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Loginwindow()
    ui.show()
    sys.exit(app.exec_())
